package cn.cstech.aijia.ui.guide;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lidroid.xutils.bitmap.core.BitmapDecoder;
import com.lidroid.xutils.bitmap.core.BitmapSize;

import java.util.ArrayList;
import java.util.List;

import cn.cstech.aijia.R;

public class GuideActivity extends Activity implements OnPageChangeListener {
    private static final String TAG = GuideActivity.class.getSimpleName();

    private GuideViewPager vp;
	private ViewPagerAdapter vpAdapter;
	private List<View> views;

//	private Button loginBtn;
//	private Button registerBtn;
	
	private boolean isHide = false;
	private boolean isWaitingInTwo = false;
	
	private ImageView indicate_1;
	private ImageView indicate_2;
	private ImageView indicate_3;
	private ImageView indicate_4;
	
	private LinearLayout ll_indicate;
	
	private Button goBtn;
	
	Bitmap bgBitmap1;
	Bitmap bgBitmap2;
	Bitmap bgBitmap3;
	Bitmap bgBitmap4;

	Dialog createShortCutDlg = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.guide_page);

        try {
            // 初始化页面
            initViews();
        } catch (Exception e) {
            Log.w(TAG, e.toString(), e);
        }
		
		if(isHide){
			hideLoginAndRegisterFunc();
		}
	}

	@SuppressLint("NewApi")
	private void initViews() {
		LayoutInflater inflater = LayoutInflater.from(this);

		views = new ArrayList<View>();
		// 初始化引导图片列表
		View view1 = inflater.inflate(R.layout.what_new_one, null);
		views.add(view1);
		
		Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth() ;
        int height = display.getHeight() ;
        BitmapSize bitmapSize = new BitmapSize(width, height);
		ImageView iv_guide_1 = (ImageView)view1.findViewById(R.id.iv_guide_1);
		
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
        	bgBitmap1 = BitmapDecoder.decodeSampledBitmapFromResource(getResources(), R.drawable.guide_1,
					bitmapSize, null);
        }
		if (bgBitmap1 != null) {
			iv_guide_1.setBackground(new BitmapDrawable(getResources(),
					bgBitmap1));
		} else {
			iv_guide_1.setBackgroundResource(R.drawable.guide_1);
		}
		View view2 = inflater.inflate(R.layout.what_new_two, null);
		views.add(view2);
		
		ImageView iv_guide_2 = (ImageView)view2.findViewById(R.id.iv_guide_2);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
        	bgBitmap2 = BitmapDecoder.decodeSampledBitmapFromResource(getResources(), R.drawable.guide_2,
					bitmapSize, null);
        }
        if (bgBitmap2 != null) {
        	iv_guide_2.setBackground(new BitmapDrawable(getResources(), bgBitmap2));
        }else{
        	iv_guide_2.setBackgroundResource(R.drawable.guide_2);
        }
        
		View view3 = inflater.inflate(R.layout.what_new_three,null);
		views.add(view3);
		
		ImageView iv_guide_3 = (ImageView)view3.findViewById(R.id.iv_guide_3);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
        	bgBitmap3 = BitmapDecoder.decodeSampledBitmapFromResource(getResources(), R.drawable.guide_3,
					bitmapSize, null);
        	iv_guide_3.setBackground(new BitmapDrawable(getResources(), bgBitmap3));
        }else{
        	iv_guide_3.setBackgroundResource(R.drawable.guide_3);
        }
        
        View view4 = inflater.inflate(R.layout.what_new_four,null);
		views.add(view4);
		
        ImageView iv_guide_4 = (ImageView)view4.findViewById(R.id.iv_guide_4);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
        	bgBitmap4 = BitmapDecoder.decodeSampledBitmapFromResource(getResources(), R.drawable.guide_4,
					bitmapSize, null);
        	iv_guide_4.setBackground(new BitmapDrawable(getResources(), bgBitmap4));
        }else{
        	iv_guide_4.setBackgroundResource(R.drawable.guide_4);
        }
        
//		if(isHide){
//			view4.setOnClickListener(new OnClickListener() {
//
//				@Override
//				public void onClick(View v) {
//					finish();
//					overridePendingTransition(R.anim.right_in, R.anim.left_out);
//				}
//			});
//		}

		// 初始化Adapter
		vp = (GuideViewPager) findViewById(R.id.viewpager);
		vpAdapter = new ViewPagerAdapter(views, vp);
		
		vp.setAdapter(vpAdapter);
		// 绑定回调
		vp.setOnPageChangeListener(this);
		
//		loginBtn = (Button)findViewById(R.id.login_btn);
		
		goBtn = (Button)view4.findViewById(R.id.go_btn);
		goBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				login();
			}
		});
//		loginBtn.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				login();
//			}
//		});
//		registerBtn = (Button)findViewById(R.id.register_btn);
//		registerBtn.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				register();
//			}
//		});
		
		ll_indicate = (LinearLayout)findViewById(R.id.ll_indicate);
		indicate_1 = (ImageView)findViewById(R.id.indicate_1);
		indicate_2 = (ImageView)findViewById(R.id.indicate_2);
		indicate_3 = (ImageView)findViewById(R.id.indicate_3);
		indicate_4 = (ImageView)findViewById(R.id.indicate_4);
	}
	
	@Override
	protected void onDestroy() {
		releaseResources();
		super.onDestroy();
	}
	
	private void releaseResources(){
		if(bgBitmap1 != null){
			bgBitmap1.recycle();
		}
		
		if(bgBitmap2 != null){
			bgBitmap2.recycle();
		}
		
		if(bgBitmap3 != null){
			bgBitmap3.recycle();
		}
		
		if(bgBitmap4 != null){
			bgBitmap4.recycle();
		}
	}

	private void selectIndicate(int index){
		switch (index) {
		case 0:
			indicate_1.setImageResource(R.drawable.guide_page_point_selected);
			indicate_2.setImageResource(R.drawable.guide_page_point_normal);
			indicate_3.setImageResource(R.drawable.guide_page_point_normal);
			indicate_4.setImageResource(R.drawable.guide_page_point_normal);
			break;
		case 1:
			indicate_2.setImageResource(R.drawable.guide_page_point_selected);
			indicate_1.setImageResource(R.drawable.guide_page_point_normal);
			indicate_3.setImageResource(R.drawable.guide_page_point_normal);
			indicate_4.setImageResource(R.drawable.guide_page_point_normal);
			break;
		case 2:
			indicate_4.setImageResource(R.drawable.guide_page_point_normal);
			indicate_3.setImageResource(R.drawable.guide_page_point_selected);
			indicate_2.setImageResource(R.drawable.guide_page_point_normal);
			indicate_1.setImageResource(R.drawable.guide_page_point_normal);
			break;
		case 3:
			indicate_4.setImageResource(R.drawable.guide_page_point_selected);
			indicate_3.setImageResource(R.drawable.guide_page_point_normal);
			indicate_2.setImageResource(R.drawable.guide_page_point_normal);
			indicate_1.setImageResource(R.drawable.guide_page_point_normal);
			break;
		default:
			break;
		}
	}
	
	private void hideLoginAndRegisterFunc(){
//		loginBtn.setVisibility(View.GONE);
//		registerBtn.setVisibility(View.GONE);
		goBtn.setVisibility(View.GONE);
	}
	
	private void login(){
//		Intent intent = new Intent(this, LoginActivity.class);
//		intent.putExtra(Preference.EXTRA_MAIN_UI,Preference.ACTION_SPLASH);
//		intent.putExtra(Preference.ACTION,Preference.ACTION_START_APP);
//		startActivity(intent);
//		finish();
	}
	
	
//	private void register(){
//		Intent intent = new Intent(this, PhoneNumberInputActivity.class);
//        intent.putExtra(Preference.ACTION, Preference.ACTION_REGISTER);
//        intent.putExtra(Preference.EXTRA_MAIN_UI, Preference.ACTION_SPLASH);
//        startActivityForResult(intent, Preference.CODE_REGISTER);
//        finish();
//	}
	
	// 当滑动状态改变时调用
	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	// 当当前页面被滑动时调用
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		if(isHide){
			if(arg0 == 2 && arg1 == 0.0f && arg2 == 0){
				if(!isWaitingInTwo){
					isWaitingInTwo = true;
				}else{
					finish();
					overridePendingTransition(R.anim.right_in, R.anim.left_out);
				}
			}
		}
	}

	// 当新的页面被选中时调用
	@Override
	public void onPageSelected(int pageIndex) {
		if(pageIndex == (views.size() - 1)){
			ll_indicate.setVisibility(View.GONE);
		}else{
			ll_indicate.setVisibility(View.VISIBLE);
		}
		selectIndicate(pageIndex);
	}
}
